﻿Imports YvanSoftware.Orb
Public Class frmLogin
    Private strAPIKey As String
    Private _strSessionID As String
    ' TODO: Insert code to perform custom authentication using the provided username and password 
    ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' The custom principal can then be attached to the current thread's principal as follows: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' where CustomPrincipal is the IPrincipal implementation used to perform authentication. 
    ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
    ' such as the username, display name, etc.
    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        Throw New Exception("Canno login without API Key.")
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Sub New(ByVal apikey As String)
        strAPIKey = apikey
        InitializeComponent()
    End Sub

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Call DoAuth(UsernameTextBox.Text, PasswordTextBox.Text)
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        _strSessionID = "AUTHFAIL_CANCELLED"

        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("This app uses " & vbCrLf & "OrbDorNet Client Library" & vbCrLf & "(c) Yvan Janssens")
    End Sub

    Sub DoAuth(ByVal username As String, ByVal password As String)
        Dim oAuth As New Authentication(strAPIKey)
        If oAuth.performLogin(username, password) Then
            _strSessionID = oAuth.sessionID
            Me.Close()
        Else
            _strSessionID = "AUTHFAIL_BADLOGIN"
            MsgBox("Login failed.")
        End If
    End Sub

    Property SessionID() As String
        Get
            Return _strSessionID
        End Get
        Set(ByVal value As String)
            'do nothing - SESSION ID SHOULD NOT BE SET FOR LOGIN WINDOW ;)
        End Set
    End Property
End Class
